//
//  TinyNumberView.swift
//  suda
//
//  Created by youai on 2019/7/19.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit

//消息用的
class TinyNumberView : UIView {
    let label = UILabel()
    init(){
        super.init(frame:.zero)
        let inset = 5
        self.drawLabel(label: label, text: "", fontSize: 11, textColor: .white) { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(inset)
            make.right.equalToSuperview().offset(-inset)
        }
        self.cornerRadius(17.0/2.0)
        self.backgroundColor = .xFF3B30
    }
    
    func setView(){
        assert(self.superview != nil)
        self.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.width.greaterThanOrEqualTo(17)
        }
    }
    
    func setValue(_ value:Int){
        if value == 0 {
            if !self.isHidden {
                self.isHidden = true
            }
        }else{
            label.text = (value > 99) ? "99+" : String(value)
            label.sizeToFit()
            if self.isHidden {
                self.isHidden = false
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//购物车用的
class TinyNumberView2 : UIView {
    let label = UILabel()
    init(){
        super.init(frame:.zero)
        let inset = 3
        self.drawLabel(label: label, text: "", fontSize: 8, textColor: .white) { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(inset)
            make.right.equalToSuperview().offset(-inset)
        }
        self.cornerRadius(11.0/2.0)
        self.backgroundColor = .xF5A623
    }
    
    func setView(){
        assert(self.superview != nil)
        self.snp.makeConstraints { (make) in
            make.height.equalTo(11.0)
            make.width.greaterThanOrEqualTo(11.0)
        }
    }
    
    func setValue(_ value:Int){
        if value == 0 {
            if !self.isHidden {
                self.isHidden = true
            }
        }else{
            label.text = (value > 99) ? "99+" : String(value)
            label.sizeToFit()
            if self.isHidden {
                self.isHidden = false
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
